package cn.yeyiu.netonlinevideo.activity;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;

import android.content.res.Configuration;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.alibaba.fastjson.JSONObject;

import cn.yeyiu.netonlinevideo.R;
import cn.yeyiu.netonlinevideo.adapter.GridDataAdapter;
import cn.yeyiu.netonlinevideo.bean.InfoData;
import cn.yeyiu.netonlinevideo.util.SourceObserver;
import cn.yeyiu.netonlinevideo.util.DataServer;
import cn.yeyiu.util.bean.MediaData;

/**
 * 一个简单的数据展示界面
 */
public class DataListActivity extends AppCompatActivity {

    private ImageView backBtn;
    private TextView backTv;
    private RecyclerView rv;
    private ProgressBar progressBar;

    private GridDataAdapter adapter;

    private MediaData typeMedia;

    // page相关
    private ImageView preIv;
    private ImageView nextIv;
    private TextView curTv;
    private TextView maxTv;

    private DataServer dataGetter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_data_list);

        dataInit();
        bindView();
        bindEvent();
        initUI();
    }

    @Override
    public void onConfigurationChanged(@NonNull Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            rv.setLayoutManager(new StaggeredGridLayoutManager(4, StaggeredGridLayoutManager.VERTICAL));
        } else {
            rv.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
        }
    }

    void dataInit() {
        typeMedia = JSONObject.parseObject(getIntent().getStringExtra("type"), MediaData.class);
        adapter = new GridDataAdapter(this);
        dataGetter = new DataServer(this, typeMedia.code);
    }

    void bindView() {
        backBtn = findViewById(R.id.back_btn);
        backTv = findViewById(R.id.back_tv);
        rv = findViewById(R.id.data_list_rv);
        progressBar = findViewById(R.id.progressBar);

        preIv = findViewById(R.id.pre_page_btn);
        nextIv = findViewById(R.id.next_page_btn);
        curTv = findViewById(R.id.cur_page_tv);
        maxTv = findViewById(R.id.sum_page_tv);

        rv.setAdapter(adapter);
        DisplayMetrics displayMetrics = getResources().getDisplayMetrics();
        int count = 2;
        if (displayMetrics.widthPixels > displayMetrics.heightPixels) {
            count = 4;
        }
        rv.setLayoutManager(new StaggeredGridLayoutManager(count, StaggeredGridLayoutManager.VERTICAL));

    }

    void bindEvent() {
        backBtn.setOnClickListener(v -> {
            finish();
        });

        dataGetter.setDatasChangeListener(new SourceObserver.DatasChangeListener() {
            @Override
            public void onDatasChange(InfoData infoData) {
                // page
                if (infoData.type.equals(typeMedia.type)) {
                    maxTv.setText(String.valueOf(infoData.maxIndex + 1));

                    adapter.updateList(infoData.dataList);
                    progressBar.setVisibility(View.GONE);
                    rv.smoothScrollToPosition(0);
                }
            }
        });

        preIv.setOnClickListener(v -> {
            changePage(false);
        });

        nextIv.setOnClickListener(v -> {
            changePage(true);
        });
    }

    void changePage(boolean next){
        progressBar.setVisibility(View.VISIBLE);
        if (next){
            dataGetter.nextPage(typeMedia.type);
        }else{
            dataGetter.prePage(typeMedia.type);
        }
        curTv.setText(String.valueOf(dataGetter.getCurPage(typeMedia.type)));

    }

    void initUI() {
        backTv.setText(typeMedia.title);
        findViewById(R.id.page_ll).setVisibility(View.VISIBLE);
        dataGetter.postFirstPageData(typeMedia.type);
    }
}